
class Chart {
  constructor(target, {...options}){
    this.element = typeof(target) === 'string' ? document.getElementById(target) : target;
    this.options = options;
    this._init();
  }

  _init(){
    this.chart = echarts.init(this.element);
    this.chart.setOption(this.options);
    this._resize = () => {
      this.chart.resize();
    };
    window.addEventListener('resize', this._resize);
  }

  setOption(option){
    this.options = option;
    this.chart.setOption(this.options);
  }

  setTitle(title){
    if (this.options.title){
      this.options.title.text = title;
    } else {
      this.options.title = {text: title};
    }
    this.chart.setOption(this.options);
  }
  setSeriesData(index, [...data]){
    this.options.series[index].data = data;
    this.chart.setOption(this.options);
  }

  // 销毁
  dispose(){
    this.element = null;
    this.options = null;
    this.chart.dispose();
    this.chart = null;
    window.removeEventListener('resize',this._resize);
  }

  updateLegendselected(selected){
    this.options.legend.selected = selected;
    this.chart.setOption(this.options);
  }


}

export default Chart;
